package com.zhouxin.customview.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.graphics.withSave
import com.zhouxin.customview.R
import com.zhouxin.customview.extensions.dp2px

private val IMAGE_WIDTH = 200f.dp2px
private val IMAGE_PADDING = 100f.dp2px

class AnimatorView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    /*private val clipped = Path().apply {
        addOval(
            IMAGE_PADDING,
            IMAGE_PADDING,
            IMAGE_PADDING + IMAGE_WIDTH,
            IMAGE_PADDING + IMAGE_WIDTH,
            Path.Direction.CW
        )
    }*/
    private val camera = Camera()
    var topFlip = 0f
        set(value) {
            field = value
            invalidate()
        }
    var bottomFlip = 0f
        set(value) {
            field = value
            invalidate()
        }
    var flipRotation = 0f
        set(value) {
            field = value
            invalidate()
        }

    init {
        //动态适配手机机型，“-6”这个值根据实际情况自己定
        camera.setLocation(0f, 0f, -6 * resources.displayMetrics.density)
    }

    override fun onDraw(canvas: Canvas) {
        /*//切一个圆形区域
        canvas.clipPath(clipped)*/

        //上半部分
        //下面倒着看好理解，实际运行时正着运行的
        canvas.withSave {
            //6.移回
            canvas.translate(IMAGE_PADDING + IMAGE_WIDTH / 2f, IMAGE_PADDING + IMAGE_WIDTH / 2f)
            //5，旋转
            canvas.rotate(-flipRotation)
            //4.翻转
            camera.save()//防止应用退到后台后再显示时叠加绘制
            camera.rotateX(topFlip)
            camera.applyToCanvas(canvas)
            camera.restore()
            //3.裁切
            canvas.clipRect(-IMAGE_WIDTH, -IMAGE_WIDTH, IMAGE_WIDTH, 0f)
            //2.旋转
            canvas.rotate(flipRotation)
            //1.移动
            canvas.translate(
                -(IMAGE_PADDING + IMAGE_WIDTH / 2f),
                -(IMAGE_PADDING + IMAGE_WIDTH / 2f)
            )
            canvas.drawBitmap(getAvatar(IMAGE_WIDTH.toInt()), IMAGE_PADDING, IMAGE_PADDING, paint)
        }

        /*下半部分*/
        canvas.withSave {
            canvas.translate(IMAGE_PADDING + IMAGE_WIDTH / 2f, IMAGE_PADDING + IMAGE_WIDTH / 2f)
            canvas.rotate(-flipRotation)
            camera.save()
            camera.rotateX(bottomFlip)
            camera.applyToCanvas(canvas)
            camera.restore()
            canvas.clipRect(-IMAGE_WIDTH, 0f, IMAGE_WIDTH, IMAGE_WIDTH)
            canvas.rotate(flipRotation)
            canvas.translate(
                -(IMAGE_PADDING + IMAGE_WIDTH / 2f),
                -(IMAGE_PADDING + IMAGE_WIDTH / 2f)
            )
            canvas.drawBitmap(getAvatar(IMAGE_WIDTH.toInt()), IMAGE_PADDING, IMAGE_PADDING, paint)
        }

    }

    private fun getAvatar(width: Int): Bitmap {
        val options = BitmapFactory.Options()
        //只测量图片尺寸
        options.inJustDecodeBounds = true
        BitmapFactory.decodeResource(resources, R.drawable.fire_bg, options)
        options.inJustDecodeBounds = false
        //原图多大
        options.inDensity = options.outWidth
        //目标多大
        options.inTargetDensity = width
        //得到的bitmap就会根据options.inDensity和options.inTargetDensity的比例缩放至需要的大小，优化了图片的加载
        return BitmapFactory.decodeResource(resources, R.drawable.fire_bg, options)
    }
}